<template>
	<view class="column">
		<view class="header">
			<image src="/static/img/mebg.png" />
		</view>
		
		<!-- <view class="row center">
			<view>登录即视为您同意</view>
			<navigator url="./selym" class="selym">《用户协议》</navigator>
		</view> -->
		<button class="bottom" type="primary" hover-class="hover-opacity"  @click="bindGetUserInfo"
			v-if="show">微信授权登录</button>
		<button class="bottom" type="primary" hover-class="hover-opacity"  open-type="getPhoneNumber" lang="zh_CN"
			@getphonenumber="bindGetPhoneNumber" v-else>进入</button>
			<mix-loading v-if="isLoading"></mix-loading>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				uid:"",
				show: true
			};
		},
		methods: {
			bindGetPhoneNumber(e) {
				console.log(e)
				let self = this;
				uni.getSetting({
					success(wxoptions) {
						if (wxoptions.authSetting['scope.userInfo']) {
							uni.login({
								success: async options => {
									console.log(options)
									let parmas = {
										encryptedData: e.detail.encryptedData,
										iv: e.detail.iv,
										code: options.code,
										uid:self.uid
									};
									const res = await self.$request('user/bindphone',parmas,{showLoading:true});
									if (res.result == 0) {
										uni.setStorageSync('uid',self.uid);
										self.back();
									} else {
										self.$util.msg(res.resultNote);
									}
								}
							})
						}
					}
				})
			},
			 bindGetUserInfo() {
				 console.log(11111)
				let self = this;
				uni.getUserProfile ({
					desc: "用于完善个人信息",
					success:e=>{
						console.log(e)
						if (e.errMsg == "getUserProfile:ok") {
							 uni.login({
								success:async res=> {
									console.log(res)
									let parmas = {
										code: res.code,
										nickName:e.userInfo.nickName,
										avatarUrl:e.userInfo.avatarUrl,
										belongTo:getApp().globalData.inviteId
									};
									const response =await self.$request('user/login',parmas,{showLoading:true});
										console.log(response)
									if (response.result == 0) {
										// 是否已经绑定手机号 0否1是
										if(response.data.isBand==0){
											self.show=false;
											self.uid=response.data.uid;
										}else{
											uni.setStorageSync('uid',response.data.uid);
											self.back();
										}
									} else {
										self.$util.msg(response.resultNote)
									}
								}
							})
						}
					}
				})
				
				
			}
		}
	}
</script>
<style>
	page {
		background: #FFFFFF;
	}
</style>
<style scoped lang="less">
	.selym{
		color:#0055ff;
	}
	.header {
		margin:60px auto;
		text-align: center;
	}

	.hide {
		opacity: 0;
	}

	.header image {
		width: 200upx;
		height: 200upx;
		border-radius: 50%;
	}

	.bottom {
		height: 80upx;
		width: 400upx;
		background: #0055ff !important;
		border-radius: 12upx;
		margin: 70upx auto;
		font-size: 30upx;
		color: #FFFFFF;
	}
</style>
